package com.xy.ad.lib

import com.xy.ad.lib.config.ProcessorConfig
import com.xy.ad_annotations.AdAction
import javax.annotation.processing.*
import javax.lang.model.SourceVersion
import javax.lang.model.element.Element
import javax.lang.model.element.ExecutableElement
import javax.lang.model.element.TypeElement
import javax.lang.model.element.VariableElement
import javax.lang.model.util.Elements
import javax.tools.Diagnostic

@SupportedSourceVersion(SourceVersion.RELEASE_8)
class ADAnnotationProcessor : AbstractProcessor(){
    private val TAG = "ADAnnotationProcessor"
    private var elements:Elements? = null
    private var messager:Messager? = null
    private var filer:Filer? = null

    override fun init(processingEnv: ProcessingEnvironment?) {
        super.init(processingEnv)
        messager?.printMessage(Diagnostic.Kind.NOTE, "$TAG --init")
        elements = processingEnv?.elementUtils
        messager = processingEnv?.messager
        filer = processingEnv?.filer
    }

    override fun getSupportedAnnotationTypes(): MutableSet<String> {
        return mutableSetOf(ProcessorConfig.SupportedAnnotationTypes)
    }

    override fun process(
        annotations: MutableSet<out TypeElement>?,
        roundEnv: RoundEnvironment?
    ): Boolean {
        if (annotations == null || annotations.isNullOrEmpty()) {
            return false
        }
        messager?.printMessage(Diagnostic.Kind.NOTE, "$TAG --process")
        //获取app中所有用到AdAction注解的对象
        val elementAnnotations = roundEnv?.getElementsAnnotatedWith(AdAction::class.java)
        elementAnnotations?.forEach {
            val typeMirror = it.asType()


        }

//        val elementsMap = mutableMapOf<Class<*>,ArrayList<Element>>()
//        elementAnnotations?.forEach {
//            println("$TAG --- $it")
//            if (it != null){
//                val className = it.enclosingElement.simpleName.toString()
//                println("$TAG --- $className")
//                val javaClass = it.enclosingElement.javaClass
//                var elementList = elementsMap[javaClass]
//                if (elementList.isNullOrEmpty()){
//                    elementList = ArrayList()
//                    elementsMap[javaClass] = elementList
//                }
//                elementList.add(it)
//            }
//        }
//
//        elementsMap.forEach { (t, u) ->
//            val element = t as ExecutableElement
//
//            println("$TAG -annotations--${element.simpleName}")
//            for (method in t.methods) {
//                val annotations = method.annotations
//                annotations.forEach {
//
////                    println("$TAG -annotations--${method.name}---- ${it.annotationClass.simpleName}")
//                }
//
//            }


//        }



        return true
    }
}